<template>
    <div class="about">
        <el-form :inline="true" :model="quertInfo" class="demo-form-inline">
            <el-form-item label="公告标题">
                <el-input v-model="quertInfo.name" placeholder="请输入题目关键字" clearable />
            </el-form-item>
            <el-form-item label="公告内容">
                <el-input v-model="quertInfo.publicitytext" placeholder="请输入内容关键字" clearable />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="getArticleList">搜索</el-button>
            </el-form-item>
        </el-form>
        <div class="mb20">
            <el-button @click="toAdd" type="primary" icon="Plus" plain>新增</el-button>
            <el-button @click="toScopeDelete" type="danger" icon="Delete" plain>删除</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column fixed prop="id" label="编号" width="150" />
            <el-table-column prop="name" label="文章标题" width="150" />
            <el-table-column prop="publicitytext" label="内容" width="150" show-overflow-tooltip>
                <template #default="scope">
                    <el-button type="text" @click="showDetails(scope.row)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="createtime" label="时间" width="150" />
            <el-table-column fixed="right" label="操作" min-width="120">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="toEdit(scope.row)">修改</el-button>
                    <el-button link type="primary" @click="onDel(scope.row.id)" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="quertInfo.pageNum" v-model:page-size="quertInfo.pageSize"
            :page-sizes="[1, 5, 10, 20]" :size="size" :disabled="disabled" :background="false"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" style="margin-top: 20px;" />
    </div>

    <!-- 添加，编辑弹框 -->
    <!-- <el-dialog v-model="showPop" :title="isEdit ? '编辑' : '添加'">
        <DicAddCom v-if="showPop" @onCancel="onCancel" :dicid="dicid" :isEdit="isEdit" />
    </el-dialog> -->
    <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500">
        <el-form :model="article">
            <!-- <el-form-item label="编号" :label-width="formLabelWidth">
                <el-input v-model="article.id" autocomplete="off" />
            </el-form-item> -->

            <el-form-item label="标题" :label-width="formLabelWidth">
                <el-input v-model="article.name" autocomplete="off" />
            </el-form-item>

            <el-form-item label="内容" :label-width="formLabelWidth">
                <!-- <el-input v-model="article.publicitytext" autocomplete="off" /> -->
                <quill-editor v-model:value="article.publicitytext" />
            </el-form-item>

            <el-form-item label="时间" :label-width="formLabelWidth">
                <el-input v-model="article.createtime" autocomplete="off" />
            </el-form-item>



        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="saveOrUpdate(row)">
                    {{ saveOrUpdateTitle }}
                </el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog v-model="detailsDialogVisible" width="800" style="height: 500px;">
        <div v-html="currentArticleDetails"></div>
    </el-dialog>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'
import QuillEditor from "@/components/QuillEditor.vue";

//变量区域开始
const selectedIds = ref([]);
const handleSelectionChange = (val) => {
    selectedIds.value = val.map(item => item.id);
}

const quertInfo = ref({
    pageNum: 1,
    pageSize: 5,
    name: '',
    publicitytext: '',
    createtime: '',
})

const total = ref(0)

const detailsDialogVisible = ref(false); // 详情内容弹框的显示状态
const currentArticleDetails = ref(''); // 当前文章的详情内容
// 显示详情内容的函数
const showDetails = (row) => {
    currentArticleDetails.value = row.publicitytext;
    detailsDialogVisible.value = true;
};


// 修改或添加
const saveOrUpdateVisible = ref(false);
const article = ref({
    id: 0,
    name: '',
    publicitytext: '',
    createtime: '',
})
const saveOrUpdateTitle = ref('添加');

const imageUrl = ref('')
//变量区域结束

//函数区域开始


//  取消修改或保存
const close = () => {
    saveOrUpdateVisible.value = false;
    imageUrl.value = '';
    article.value = {
        id: 0,
        name: '',
        publicitytext: '',
        createtime: '',
    }
}

const toScopeDelete = () => {
    if (selectedIds.value.length == 0) {
        ElMessage({
            message: '请选择要删除的记录！',
            type: 'error',
        })
        return;
    } else {
        ElMessageBox.confirm(
            '真的要删除吗？',
            '提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(() => {
                //调用后端删除接口
                axios.delete('/publicity/deleteByIds',
                    {
                        data: selectedIds.value
                    }
                )
                    .then(res => {
                        let result = res.data;
                        if (result.code == 0) {
                            ElMessage({
                                message: '删除成功！',
                                type: 'success',
                            })
                            getcoursetypeList();

                        } else {
                            ElMessage({
                                message: '删除失败！',
                                type: 'error',
                            })
                            return;
                        }
                    })
            })
    }
}

//  保存或更新
const saveOrUpdate = () => {
    if (saveOrUpdateTitle.value == '添加') {
        console.log("添加")
        // 调用后端接口添加
        // 
        axios.post('/publicity', article.value)
            .then(resp => {
                // console.log(resp.data)
                let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
                if (result.code == 1) {
                    getArticleList();
                } else {
                    console.log('添加文章失败')
                }
            })
    } else {
        // 调用后端接口修改
        axios.put('/publicity', article.value)
            .then(resp => {
                // console.log(resp.data)
                let result = resp.data;  // resp是promise对象，.data返回我们自己的标准R
                if (result.code == 1) {
                    // 重新获取列表
                    getArticleList();
                } else {
                    console.log('修改文章失败')
                }
            })
    }
    // 关闭弹框
    saveOrUpdateVisible.value = false;
    imageUrl.value = '';
    article.value = {
        id: 0,
        name: '',
        publicitytext: '',
        createtime: '',
    }
}

//  去修改的页面
const toEdit = row => {
    saveOrUpdateVisible.value = true;
    saveOrUpdateTitle.value = '编辑';
    article.value = row;

    console.log('article', article.value)
}

//  去添加的页面
const toAdd = row => {
    saveOrUpdateVisible.value = true;
    saveOrUpdateTitle.value = '添加';
    article.value = {
        id: 0,
        name: '',
        publicityText: '',
        createTime: '',
    }
}

const getArticleList = () => {
    //使用axios请求数据
    axios.get('/publicity/selectAll', {
        params: quertInfo.value
    })
        .then(res => {
            let result = res.data;
            if (result.code == 1) {
                tableData.value = result.data.list;
                total.value = result.data.total;
            } else {
                console.log('获取公告失败')
            }
        })
        .catch(err => {
            console.log('err', err);
        })
}
const handleSizeChange = (val) => {
    quertInfo.value.pageSize = val;
    quertInfo.value.pageNum = 1;
    getArticleList();
}
const handleCurrentChange = (val) => {
    quertInfo.value.pageNum = val;
    getArticleList();
}
//函数区域结束

//钩子函数区域开始
onMounted(() => {
    console.log('onMounted');
    getArticleList();
})
//钩子函数区域结束

const formInline = reactive({
    name: '',
    state: '',
    date: '',
})

// 删除
const onDel = id => {
    console.log('id', id);
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            console.log('确定');
            //调用后端删除接口
            axios.delete('/publicity/' + id)
                .then(res => {
                    let result = res.data;  // resp是promise对象，.data返回我们自己的标准R 对象
                    if (result.code != 1) {
                        ElMessage({
                            message: '删除失败！',
                            type: 'error',
                        })
                        return;
                    } else {
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                        getArticleList();
                    }
                })

        })
        .catch(() => { })
}

const tableData = ref([
    {
        id: 1,
        name: '颜色',
        state: 1,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
    {
        id: 2,
        name: '性别',
        state: 0,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
    {
        id: 3,
        name: '车牌',
        state: 1,//1可用，0禁用
        mark: 'Los Angeles',
        date: '2016-05-03',
    },
])
</script>
<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.about {
    margin-left: 20px;
}
</style>